<!doctype html>
<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Test Udx Javascript</title>
  </head>
  <body>
    <textarea id="output" rows="8"></textarea>
	
	<input type="text" value="" />
	<input type="button" value="test function" style="width:128px; height:24px" onclick="sasa()" />
	<script type="text/javascript" src="nxdat.data.js"></script>
	
	<script type="text/javascript">
		var EKernelType = {
			EKT_NULL: Module.KernelType.EKT_NULL,
			EKT_INT: Module.KernelType.EKT_INT,
			EKT_REAL: Module.KernelType.EKT_REAL,
			EKT_STRING: Module.KernelType.EKT_STRING,
			EKT_VECTOR2: Module.KernelType.EKT_VECTOR2,
			EKT_VECTOR3: Module.KernelType.EKT_VECTOR3,
			EKT_VECTOR4: Module.KernelType.EKT_VECTOR4,
			EKT_INT_LIST: Module.KernelType.EKT_INT_LIST,
			EKT_REAL_LIST: Module.KernelType.EKT_REAL_LIST,
			EKT_STRING_LIST: Module.KernelType.EKT_STRING_LIST,
			EKT_VECTOR2_LIST: Module.KernelType.EKT_VECTOR2_LIST,
			EKT_VECTOR3_LIST: Module.KernelType.EKT_VECTOR3_LIST,
			EKT_VECTOR4_LIST: Module.KernelType.EKT_VECTOR4_LIST,
			EKT_NODE: Module.KernelType.EKT_NODE,
			EKT_LIST: Module.KernelType.EKT_LIST,
			EKT_MAP: Module.KernelType.EKT_MAP,
			EKT_TABLE: Module.KernelType.EKT_TABLE
		};
		
		function Vector2d(_x, _y){
			this.x = _x;
			this.y = _y;
		}
		function Vector3d(_x, _y, _z){
			this.x = _x;
			this.y = _y;
			this.z = _z;
		}
		function Vector4d(_x, _y, _z, _m){
			this.x = _x;
			this.y = _y;
			this.z = _z;
			this.m = _m;
		}
		
		function UdxKernel(pType, pNode){
			this.mType = pType;
			this.mNode = pNode;
			
			this.getNode=function(){
				return this.mNode;
			};
			
			this.getType=function(){
				return this.mType;
			};
			
			this.getTypedValue=function(){
				if (this.mType == EKernelType.EKT_INT){
					return Module.getNodeIntValue(this.mNode.getNativeObj());
				}
				else if (this.mType == EKernelType.EKT_REAL){
					return Module.getNodeRealValue(this.mNode.getNativeObj());
				}
				else if (this.mType == EKernelType.EKT_STRING){
					return Module.getNodeStringValue(this.mNode.getNativeObj());
				}
				else if (this.mType == EKernelType.EKT_VECTOR2){
					return Module.getNodeVector2dValue(this.mNode.getNativeObj());
				}
				else if (this.mType == EKernelType.EKT_VECTOR3){
					return Module.getNodeVector3dValue(this.mNode.getNativeObj());
				}
				else if (this.mType == EKernelType.EKT_VECTOR4){
					return Module.getNodeVector4dValue(this.mNode.getNativeObj());
				}
			};
			
			this.getTypedValueByIndex=function(idx){
				if (this.mType == EKernelType.EKT_INT_LIST){
					return Module.getNodeIntArrayValue(this.mNode.getNativeObj(), idx);
				}
				else if (this.mType == EKernelType.EKT_REAL_LIST){
					return Module.getNodeRealArrayValue(this.mNode.getNativeObj(), idx);
				}
				else if (this.mType == EKernelType.EKT_STRING_LIST){
					return Module.getNodeStringArrayValue(this.mNode.getNativeObj(), idx);
				}
				else if (this.mType == EKernelType.EKT_VECTOR2_LIST){
					return Module.getNodeVector2dArrayValue(this.mNode.getNativeObj(), idx);
				}
				else if (this.mType == EKernelType.EKT_VECTOR3_LIST){
					return Module.getNodeVector3dArrayValue(this.mNode.getNativeObj(), idx);
				}
				else if (this.mType == EKernelType.EKT_VECTOR4_LIST){
					return Module.getNodeVector4dArrayValue(this.mNode.getNativeObj(), idx);
				}
			};
			
			this.setTypedValue=function(val){
				if (this.mType == EKernelType.EKT_INT){
					Module.setIntNodeValue(this.mNode.getNativeObj(), val);
				}
				else if (this.mType == EKernelType.EKT_REAL){
					Module.setRealNodeValue(this.mNode.getNativeObj(), val);
				}
				else if (this.mType == EKernelType.EKT_STRING){
					Module.setStringNodeValue(this.mNode.getNativeObj(), val);
				}
				else if (this.mType == EKernelType.EKT_VECTOR2){
					Module.setVector2dNodeValue(this.mNode.getNativeObj(), val.x, val.y);
				}
				else if (this.mType == EKernelType.EKT_VECTOR3){
					Module.setVector3dNodeValue(this.mNode.getNativeObj(), val.x, val.y, val.z);
				}
				else if (this.mType == EKernelType.EKT_VECTOR4){
					Module.setVector4dNodeValue(this.mNode.getNativeObj(), val.x, val.y, val.z, val.m);
				}
			};
			
			this.addTypedValue=function(val, idx){
				if (this.mType == EKernelType.EKT_INT_LIST){
					Module.addIntNodeValue(this.mNode.getNativeObj(), val, idx);
				}
				else if (this.mType == EKernelType.EKT_REAL_LIST){
					Module.addRealNodeValue(this.mNode.getNativeObj(), val, idx);
				}
				else if (this.mType == EKernelType.EKT_STRING_LIST){
					Module.addStringNodeValue(this.mNode.getNativeObj(), val, idx);
				}
				else if (this.mType == EKernelType.EKT_VECTOR2_LIST){
					Module.addVector2dNodeValue(this.mNode.getNativeObj(), val.x, val.y, idx);
				}
				else if (this.mType == EKernelType.EKT_VECTOR3_LIST){
					Module.addVector3dNodeValue(this.mNode.getNativeObj(), val.x, val.y, val.z, idx);
				}
				else if (this.mType == EKernelType.EKT_VECTOR4_LIST){
					Module.addVector4dNodeValue(this.mNode.getNativeObj(), val.x, val.y, val.z, val.m, idx);
				}
			}
		}
		
		function UdxNode(pParentNode){
			this.mParentNode = pParentNode;
			
			this.create=function(pName, pType){
				this.nativeObj = Module.addChildNode(pParentNode.getNativeObj(), pName, pType);
				this.kernel = new UdxKernel(pType, this);
			}
			
			this.getNativeObj=function(){
				return this.nativeObj;
			};
			
			this.getName=function(){
				return Module.getNodeName(this.nativeObj);
			};
			
			this.setName=function(name){
				Module.setNodeName(this.nativeObj, name);
			};
			
			this.getKernel=function(){
				return this.kernel;
			};
						
			this.getChildNodeCount=function(){
				return Module.getNodeChildCount(this.nativeObj);
			};
			
			this.getChildNode=function(idx){
				var count = this.getChildNodeCount();
				if (idx<0 || idx >= count)
					return undefined;
				var node_native_obj = Module.getChildNode(this.nativeObj,idx);
				var pType = Module.getNodeType(node_native_obj);
				var pUdxNode = new UdxNode(this);
				pUdxNode.nativeObj = node_native_obj;
				pUdxNode.kernel = new UdxKernel(pType, pUdxNode);
				return pUdxNode;
			};
			
			this.addChildNode=function(name, type){
				var childNode = new UdxNode(this);
				childNode.create(name, type);
				if (childNode.nativeObj==0) return undefined;
				return childNode;
			};
		};
		
		function UdxDataset(){		
			this.createDataset=function(){
				this.dataset = Module.createDataset();
				this.nativeObj = Module.getDatasetNode(this.dataset);
				this.kernel = new UdxKernel(EKernelType.EKT_NODE, this);
			};
			
			this.getDatasetObj=function(){
				return this.dataset;
			};
			
			this.formatToXmlStream=function(){
				var xml_str = Module.formatToXmlStream1(this.dataset);
				return xml_str;
			};
			
			this.loadFromXmlStream=function(xml_str){
				Module.loadFromXmlStream1(this.dataset, xml_str);
			};
		}
		
		UdxDataset.prototype = new UdxNode();	
	</script>

    <script type='text/javascript'>
	  function sasa(){
		var dataset = new UdxDataset();
		dataset.createDataset();
		var node1 = dataset.addChildNode("Node1", EKernelType.EKT_INT);
		var node2 = dataset.addChildNode("Node2", EKernelType.EKT_REAL);
		var node3 = dataset.addChildNode("Node3", EKernelType.EKT_STRING);
		var node4 = dataset.addChildNode("Node4", EKernelType.EKT_VECTOR2);
		var node5 = dataset.addChildNode("Node5", EKernelType.EKT_VECTOR3);
		var node6 = dataset.addChildNode("Node6", EKernelType.EKT_VECTOR4);
		
		var node7 = dataset.addChildNode("Node7", EKernelType.EKT_NODE);
		var node7_1 = node7.addChildNode("node7_1", EKernelType.EKT_INT_LIST);
		var node7_2 = node7.addChildNode("node7_2", EKernelType.EKT_REAL_LIST);
		var node7_3 = node7.addChildNode("node7_3", EKernelType.EKT_STRING_LIST);
		var node7_4 = node7.addChildNode("node7_4", EKernelType.EKT_VECTOR2_LIST);
		var node7_5 = node7.addChildNode("node7_5", EKernelType.EKT_VECTOR3_LIST);
		var node7_6 = node7.addChildNode("node7_6", EKernelType.EKT_VECTOR4_LIST);
		
		node1.getKernel().setTypedValue(10);
		node2.getKernel().setTypedValue(10.23);
		node3.getKernel().setTypedValue("dfafad");
		node4.getKernel().setTypedValue(new Vector2d(12.23,98.87));
		node5.getKernel().setTypedValue(new Vector3d(12.23,98.87,45.56));
		node6.getKernel().setTypedValue(new Vector4d(12.23,98.87,45.56,34.78));
		
		node7_1.getKernel().addTypedValue(1,0);
		node7_1.getKernel().addTypedValue(2,1);
		node7_1.getKernel().addTypedValue(3,2);
		
		node7_2.getKernel().addTypedValue(1.2,0);
		node7_2.getKernel().addTypedValue(2.3,1);
		node7_2.getKernel().addTypedValue(3.4,2);
		
		node7_3.getKernel().addTypedValue("GRASS",0);
		node7_3.getKernel().addTypedValue("CCSDF",1);
		node7_3.getKernel().addTypedValue("BUILDING",2);
		
		node7_4.getKernel().addTypedValue(new Vector2d(9.8,0.87),0);
		node7_4.getKernel().addTypedValue(new Vector2d(7.6,0.65),1);
		node7_4.getKernel().addTypedValue(new Vector2d(5.4,0.43),2);
		
		node7_5.getKernel().addTypedValue(new Vector3d(9.8,0.87,34.87),0);
		node7_5.getKernel().addTypedValue(new Vector3d(7.6,0.65,45.65),1);
		node7_5.getKernel().addTypedValue(new Vector3d(5.4,0.43,56.43),2);
		
		node7_6.getKernel().addTypedValue(new Vector4d(9.8,0.87,34.87,4.387),0);
		node7_6.getKernel().addTypedValue(new Vector4d(7.6,0.65,45.65,5.465),1);
		node7_6.getKernel().addTypedValue(new Vector4d(5.4,0.43,56.43,6.543),2);
		
		var xml_str = dataset.formatToXmlStream();
		console.log(xml_str);
		
		return;
		
		node7_4.getKernel().addTypedValue(new Vector2d(100,200),1);
		node7_5.getKernel().addTypedValue(new Vector3d(100,200,300),1);
		xml_str = dataset.formatToXmlStream();
		console.log(xml_str);
		
		var temp_node0 = dataset.getChildNode(0);
		var node_name0 = temp_node0.getName();
		console.log(node_name0);
		var node_type0 = temp_node0.getKernel().getType();
		console.log(node_type0);
		
		temp_node0.getKernel().setTypedValue(43);
		xml_str = dataset.formatToXmlStream();
		console.log(xml_str);
		
		////////////////////////////////////////////////////
		var dataset1 = new UdxDataset();
		dataset1.createDataset();
		dataset1.loadFromXmlStream(xml_str);
		
		var temp_node1 = dataset1.getChildNode(0);
		var node_name = temp_node1.getName();
		console.log(node_name);
		var node_type = temp_node1.getKernel().getType();
		console.log(node_type);
		
		temp_node1.getKernel().setTypedValue(98);
		xml_str = dataset1.formatToXmlStream();
		console.log(xml_str);
	  }
	  
	  
	  function sasa1(){
		//alert("Hello");
		
		var udx_version = Module.getVersion();
		//alert(udx_version);
		
		var dataset = Module.createDataset();
		var rootNode = Module.getDatasetNode(dataset);
		
		var node0 = Module.addChildNode(rootNode, "node0", Module.KernelType.EKT_NODE);
		var node0_0 = Module.addChildNode(node0, "node0_node0", Module.KernelType.EKT_INT);
		var node0_1 = Module.addChildNode(node0, "node0_node1", Module.KernelType.EKT_REAL);
		var node0_2 = Module.addChildNode(node0, "node0_node2", Module.KernelType.EKT_STRING);
		var node0_3 = Module.addChildNode(node0, "node0_node3", Module.KernelType.EKT_VECTOR2);
		var node0_4 = Module.addChildNode(node0, "node0_node4", Module.KernelType.EKT_VECTOR3);
		var node0_5 = Module.addChildNode(node0, "node0_node5", Module.KernelType.EKT_VECTOR4);
		Module.setIntNodeValue(node0_0, 12);
		Module.setRealNodeValue(node0_1, 12.23);
		Module.setStringNodeValue(node0_2, "woshiwho");
		Module.setVector2dNodeValue(node0_3, 89.0, 98.0);
		Module.setVector3dNodeValue(node0_4, 89.0, 98.0, 78.0);
		Module.setVector4dNodeValue(node0_5, 89.0, 98.0, 78.0, 67.0);
		
		var node1 = Module.addChildNode(rootNode, "node1", Module.KernelType.EKT_INT);
		var ret1 = Module.setIntNodeValue(node1, 12);
		
		var node2 = Module.addChildNode(rootNode, "node2", Module.KernelType.EKT_REAL);
		Module.setRealNodeValue(node1, 1.23);
		
		var count = Module.getNodeChildCount(rootNode);
		var name = Module.getNodeName(node0_2);
		var ktype = Module.getNodeType(node0_2);
		var ktypeStr = Module.kerneltype2string(ktype);
		var kernelContent = Module.getNodeStringValue(node0_2);
		
		alert(count +","+ name +","+ ktypeStr +","+ length +","+ kernelContent);
		
		
		var dataset1 = Module.createDataset();
		var preload_xml = 	'<dataset>'+
								'<XDO name="node0" kernelType="any">'+
									'<XDO name="node0_node0" kernelType="int" value="12"/>'+
									'<XDO name="node0_node1" kernelType="real" value="12.23"/>'+
									'<XDO name="node0_node2" kernelType="string" value="woshiwho"/>'+
									'<XDO name="node0_node3" kernelType="vector2d" value="89.000000,98.000000"/>'+
									'<XDO name="node0_node4" kernelType="vector3d" value="89.000000,98.000000,78.000000"/>'+
									'<XDO name="node0_node5" kernelType="vector4d" value="89.000000,98.000000,78.000000,67.000000"/>'+
									'<XDO name="node0_node6" kernelType="real_array" value="89.000000,98.000000,78.000000,67.000000"/>'+
								'</XDO>'+
								'<XDO name="node1" kernelType="int" value="12"/>'+
								'<XDO name="node2" kernelType="real" value="0"/>'+
							'</dataset>';
		var load_info = Module.loadFromXmlStream(dataset1, preload_xml)
		console.log(load_info);
		
		var rootNode1 = Module.getDatasetNode(dataset1);
		var firstNode = Module.getChildNode(rootNode1,0);
		var count1 = Module.getNodeChildCount(firstNode);
		//alert(count1);
		console.log(count1);
		
		
		var ss = Module.formatToXmlStream(dataset1);
		console.log(ss);
		
	  }
    </script>
  </body>
</html>
